package isi_ib;

import java.util.Random;

import utiles_isi.dateutil;

import com.ib.client.Contract;
import com.ib.client.EClientSocket;
import com.ib.client.Order;

public class reltime_bars_ib {
	
	int tickerId;
	private Contract contract;
	private int barSizeSetting;//1 sec, 5 secs,15 secs,30 secs,1 min,2 mins,3 mins,5 mins,15 mins,30 mins,1 hour,1 day
	private String whatToShow;	// TRADES,MIDPOINT,BID,ASK,	BID_ASK,HISTORICAL_VOLATILITY,OPTION_IMPLIED_VOLATILITY
	private int useRTH;
	private barras_trade barras_rt;
	
	//0 - all data is returned even where the market in question was outside of its regular trading hours.
	//1 - only data within the regular trading hours is returned, even if the requested time span falls partially or completely outside of the RTH.
	
	//barras_acumulador acumulado; // para acumular barras en bd
	EClientSocket   client_s;
	
	public reltime_bars_ib() {
		super();
		setReltime_bars_ib(0,null,5,null,300);
		//acumulado = new barras_acumulador(5,300,true);
		
	}
	
	public reltime_bars_ib(int tickerId, Contract contract, 
			 int barSizeSetting,EClientSocket client_s_,int periodo_) {
		super();
		setReltime_bars_ib(tickerId,contract,barSizeSetting,client_s_,periodo_);
	}
	
	public void setReltime_bars_ib(int tickerId, Contract contract, int barSizeSetting,EClientSocket client_s_,int periodo_) {
		client_s=client_s_;
		setTickerId(tickerId);
		setUseRTH(1);
		this.contract = contract;
		this.barSizeSetting = barSizeSetting;
		setWhatToShow("TRADES"); 
		barras_rt= new barras_trade();
		barras_rt.setPeriodo(periodo_);
	}
	
	public int getTickerId() {
		return tickerId;
	}
	
	public void setTickerId(int tickerId) {
		if(tickerId==0)
		{
			Random randomGenerator = new Random();
			tickerId=randomGenerator.nextInt(10000000);
		}
		this.tickerId = tickerId;
	}
	public Contract getContract() {
		return contract;
	}
	public void setContract(Contract contract) {
		this.contract = contract;
	}
	public void setClientSocket(EClientSocket  client_s_) {
		this.client_s = client_s_;
	}
	public int getBarSizeSetting() {
		if(barSizeSetting==0) return 5;
		else	return barSizeSetting;
	}
	public void setBarSizeSetting(int barSizeSetting) {
		this.barSizeSetting = barSizeSetting;
	}
	public String getWhatToShow() {
		return whatToShow;
	}
	public void setWhatToShow(String whatToShow) {
		this.whatToShow = whatToShow;
	}
	public boolean getUseRTH() {
		return (useRTH==1);
	}
	public void setUseRTH(int useRTH) {
		this.useRTH = useRTH;
	}
	public void iniciaRealtimebars(EClientSocket   client_s_){
		if(client_s_!=null) setClientSocket(client_s_);
		if(client_s!=null && client_s.isConnected())
			client_s.reqRealTimeBars(tickerId,contract,getBarSizeSetting(),getWhatToShow(),getUseRTH());
	}
	public void CancelRealtimebars(EClientSocket   client_s){
			client_s.cancelRealTimeBars (tickerId);
			
		}

	@Override
	public String toString() {
		return "historico_ib [contract=" + contract + ", TickerId()="
				+ ", BarSizeSetting=" + getBarSizeSetting()
				+ ", WhatToShow=" + getWhatToShow() + ", getUseRTH="
				+ getUseRTH() + "]";
	}
	public void Add(barra_trade barra_){
		//return acumulado.add(barra_);
		barras_rt.add(barra_);
	}
	
	public void OrdenCompra(){
		      /* Order order = new Order();
		       order.m_action = "BUY";
		       order.m_totalQuantity = 1;
		       order.m_orderType = "LMT";
		       order.m_lmtPrice = 1429.0;
		       client_s.placeOrder(5655441,this.contract ,order);*/
		}
	public void OrdenCAncela(){
		     //  client_s.cancelOrder(5655441);
	}
	
}
